package cn.enilu.flash.api.controller.shop;

import cn.enilu.flash.api.controller.system.UserController;
import cn.enilu.flash.bean.constant.factory.PageFactory;
import cn.enilu.flash.bean.dto.QueryWriteOffDTO;
import cn.enilu.flash.bean.entity.shop.Goods;
import cn.enilu.flash.bean.entity.shop.Order;
import cn.enilu.flash.bean.entity.shop.OrderItem;
import cn.enilu.flash.bean.entity.system.User;
import cn.enilu.flash.bean.enumeration.shop.OrderEnum;
import cn.enilu.flash.bean.exception.ApplicationException;
import cn.enilu.flash.bean.exception.ApplicationExceptionEnum;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.bean.vo.shop.GetUserIdVO;
import cn.enilu.flash.security.UserService;
import cn.enilu.flash.service.shop.GoodsService;
import cn.enilu.flash.service.shop.OrderItemService;
import cn.enilu.flash.service.shop.OrderService;
import cn.enilu.flash.service.system.ManagerService;
import cn.enilu.flash.utils.DateUtil;
import cn.enilu.flash.utils.Lists;
import cn.enilu.flash.utils.StringUtil;
import cn.enilu.flash.utils.factory.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/shop/order/item")
public class OrderItemController {
	private  Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private OrderItemService orderItemService;
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private ManagerService managerService;
	@Autowired
	private OrderService orderService;

	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public Object list() {
	Page<OrderItem> page = new PageFactory<OrderItem>().defaultPage();
		page = orderItemService.queryPage(page);
		return Rets.success(page);
	}

	/**
	 * 已核销列表  --- 分页
 	 * @author wuchaohong
	 */
	@RequestMapping(value = "/queryWriteOffList",method = RequestMethod.POST)
	public Object queryWriteOffList(@RequestBody QueryWriteOffDTO queryWriteOffDTO) {
		Page<OrderItem> page = new PageFactory<OrderItem>().defaultPage();
		if(null != queryWriteOffDTO.getGoodsCreat() && queryWriteOffDTO.getGoodsCreat() != 0){
			page.addFilter("goodsCreat", SearchFilter.Operator.LIKE,queryWriteOffDTO.getGoodsCreat());
		}
		if(null != queryWriteOffDTO.getDateFrom()){
			page.addFilter("createTime", SearchFilter.Operator.GTE, queryWriteOffDTO.getDateFrom());
		}
		if(null != queryWriteOffDTO.getDateTo()){
			page.addFilter("createTime", SearchFilter.Operator.LTE, queryWriteOffDTO.getDateTo());
		}
		page.addFilter("writeOffStatus", SearchFilter.Operator.LIKE,1);

		page.setSort(Sort.by(Sort.Direction.DESC, "createTime"));
		page = orderItemService.queryPage(page);
		return Rets.success(page);
	}

	/**
	 * 未核算列表  --- 不分页
	 * @author wuchaohong
	 */
	@RequestMapping(value = "/queryUnWriteOffList",method = RequestMethod.POST)
	public Object queryUnWriteOffList(@RequestBody QueryWriteOffDTO queryWriteOffDTO) {
		List<SearchFilter> filters;
		if(null == queryWriteOffDTO.getGoodsCreat()||queryWriteOffDTO.getGoodsCreat() == 0 ){
			filters = Lists.newArrayList(
					SearchFilter.build("payStatus", 1),
					SearchFilter.build("writeOffStatus", 0)
			);
		}else {
			filters = Lists.newArrayList(
					SearchFilter.build("goodsCreat", queryWriteOffDTO.getGoodsCreat()),
					SearchFilter.build("payStatus", 1),
					SearchFilter.build("writeOffStatus", 0)
			);
		}
		List<OrderItem> orderItemList = orderItemService.queryAll(filters,Sort.by(Sort.Direction.DESC,"createTime"));
		//过滤退款订单
		List<OrderItem> orderItemList1 = orderItemList.stream().filter(l->{
			Order order = orderService.get(l.getIdOrder());
			if(order.getStatus().equals(OrderEnum.OrderStatusEnum.SENDED.getId())||order.getStatus().equals(OrderEnum.OrderStatusEnum.FINISHED.getId())){
				return true;
			}else{
				return false;
			}
		}).collect(Collectors.toList());
		orderItemList1.forEach(l->{
			Goods goods = goodsService.get(l.getIdGoods());
			if(null != goods.getWriteOffRate()) {
				l.setWriteOffRate(goods.getWriteOffRate());
				BigDecimal writeOffMoney = goods.getWriteOffRate().movePointLeft(2).multiply(l.getTotalPrice());
				l.setWriteOffMoney(writeOffMoney);
			}
		});
		return Rets.success(orderItemList1);
	}

	/**
	 * 核销
	 * @author wuchaohong
	 */
	@RequestMapping(value = "/writeOff",method = RequestMethod.POST)
	public Object writeOff(@RequestParam(value = "itemIds") String itemIds){
		List<SearchFilter> filters = Lists.newArrayList(
				SearchFilter.build("id", SearchFilter.Operator.IN, StringUtil.splitForLong(itemIds, ","))
		);
		List<OrderItem> orderItemList = orderItemService.queryAll(filters);
		orderItemList.forEach(l->{
			Goods goods = goodsService.get(l.getIdGoods());
			BigDecimal writeOffRate = goods.getWriteOffRate();
			if(null == writeOffRate){
				throw new ApplicationException(ApplicationExceptionEnum.NOT_WRITE_OFF_RATE);
			}
			BigDecimal writeOffMoney = writeOffRate.movePointLeft(2).multiply(l.getTotalPrice());
			l.setWriteOffMoney(writeOffMoney);
			l.setWriteOffStatus(1);
			l.setWriteOffRate(writeOffRate);
			l.setWriteOffTime(new Date());
		});
		orderItemService.update(orderItemList);
		return Rets.success();
	}

	/**
	 * 设置核销比
	 * @author wuchaohong
	 */
	@RequestMapping(value = "/setWriteOffRate",method = RequestMethod.POST)
	public Object setWriteOffRate(@RequestParam(value="rate") BigDecimal rate,@RequestParam(value = "goodsId") Long goodsId){
		Goods goods = goodsService.get(goodsId);
		goods.setWriteOffRate(rate);
		goodsService.update(goods);
		return Rets.success();
	}

	/**
	 * 获取系统人员ID
	 * @author wuchaohong
	 */
	@RequestMapping(value = "getSysUser",method = RequestMethod.GET)
	public Object getSysUser(){
		List<SearchFilter> filters = Lists.newArrayList(
				SearchFilter.build("id", SearchFilter.Operator.NE, 1),
				SearchFilter.build("id", SearchFilter.Operator.NE, 2),
				SearchFilter.build("id", SearchFilter.Operator.NE, 4),
				SearchFilter.build("id", SearchFilter.Operator.NE, 5),
				SearchFilter.build("id", SearchFilter.Operator.NE, 7),
				SearchFilter.build("status",1)
		);
		List<User> userList = managerService.queryAll(filters);
		List<GetUserIdVO> getUserIdVOList = new ArrayList<>();
		userList.forEach(l->{
			GetUserIdVO getUserIdVO = new GetUserIdVO();
			getUserIdVO.setId(l.getId());
			getUserIdVO.setName(l.getName());
			getUserIdVO.setNameAndAccount(l.getName()+l.getAccount());
			getUserIdVOList.add(getUserIdVO);
		});
		return Rets.success(getUserIdVOList);
	}
}
